<template>
  <div>
    <!-- 碎片列表 -->
    <my-breadcrumb :items="['抽奖设置', '奖品列表']"/>
    <search :formItems='searchItems' @search='getList' ref='search' />
    <div style="width: 100%;display: flex;">
      <el-button type="primary" class="margin10" @click="getNoItem" >无中奖配置</el-button>
      <el-button type="primary" class="margin10" @click="getLuckdrawconfig">活动开始/关闭</el-button>
      <el-button type="primary" class="margin10" @click="handleAddShow" >新增奖品</el-button>
      <el-button type="primary" style="margin: 10px 0px 10px auto;" @click="getLotteryList">抽奖列表</el-button>
    </div>
    <el-table :data='tableData' style='width: 100%' @sort-change="sortChange">
      <el-table-column prop="id" label='奖品id' ></el-table-column>
      <!-- <el-table-column prop='createtime' sortable label='创建时间' ></el-table-column> -->
      <el-table-column prop='type' label='奖品类型'>
        <template slot-scope="scope">
          <span v-if="scope.row.type === '1'">实体奖品</span>
          <span v-if="scope.row.type === '2'">优惠卷</span>
          <span v-if="scope.row.type === '3'">现金奖</span>
        </template>
      </el-table-column>
      <el-table-column prop='name' label='奖品名称'></el-table-column>
      <el-table-column prop='img' label='奖品图片'>
        <template slot-scope="scope">
            <img :src="scope.row.img" alt="" width="40">
        </template>
      </el-table-column>
      <el-table-column prop='probability' sortable label='中奖概率%'></el-table-column>
      <el-table-column prop='need' sortable label='所需碎片'></el-table-column>
      <el-table-column prop='stock' sortable label='奖品数量'></el-table-column>
      <el-table-column prop='createtime' sortable label='开始时间'></el-table-column>
      <el-table-column prop='endtime' sortable label='失效时间'></el-table-column>
      <el-table-column prop='gathered' sortable label='已集齐数量'></el-table-column>
      <el-table-column prop='exchange' sortable label='已兑换数量'></el-table-column>
      <el-table-column prop='status' label='状态'>
        <template slot-scope="scope">
          <span v-if="scope.row.status === '0'">失效</span>
          <span v-if="scope.row.status === '1'">生效</span>
        </template>
      </el-table-column>
      <el-table-column prop='status' label='操作'>
        <template slot-scope="scope">
          <span class="astyle"  @click="editItem(1, scope.row.id)">编辑</span>
          <span class="astyle" v-if="(scope.row.status === '1' || scope.row.status === 1) && (scope.row.isnostatus === '0' || scope.row.isnostatus === 0)"  @click="invalidItem(1, scope.row.id)">失效</span>
          <span style="color: gray;" v-if="scope.row.isnostatus === '1' || scope.row.isnostatus === 1">失效</span>
          <span class="dstyle"  @click="deleteItem(1, scope.row.id)">删除</span>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.page"
        :page-sizes="[20, 50, 100]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>

    <el-dialog
      :close-on-click-modal="false"
      :title="isAdd ? '新增奖品' : '修改奖品'"
      :visible.sync="addShow"
    >
      <!-- form start -->
      <el-form :model="diaFromData" :rules="rules" label-position="left" ref="ruleForm" label-width="90px">
          <el-form-item label="奖品类型" prop="type">
              <el-radio-group v-model="diaFromData.type">
                <el-radio :disabled='!isAdd' :label="1">实体奖品</el-radio>
                <el-radio :disabled='!isAdd' :label="2">优惠卷</el-radio>
                <el-radio :disabled='!isAdd' :label="3">现金奖</el-radio>
              </el-radio-group>
          </el-form-item>
          <el-form-item label="奖品名称"  prop="name">
            <el-input v-model="diaFromData.name" maxlength="10" ></el-input>
          </el-form-item>
          <el-form-item label="奖品图片" prop="img" style="width: 500px;">
            <el-upload
              class="avatar-uploader"
              action="#"
              :show-file-list="false"
              :before-upload="beforeUpload">
              <img v-if="diaFromData.img" :src="diaFromData.img" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="概率(%)" prop="probability">
            <el-input v-model="diaFromData.probability" type="number" ></el-input>
          </el-form-item>
          <el-form-item label="数量" prop="stock">
            <el-input  @change="changeImgOrLinkArr" v-model.number="diaFromData.stock" min="0"></el-input>
          </el-form-item>

          <template v-if="!isAdd">
            <el-form-item label="状态" prop="type">
              <el-radio-group v-model="diaFromData.status">
                <el-radio :label="0">失效</el-radio>
                <el-radio :label="1">生效</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="已集齐数量" prop="stock">
              <el-input  v-model.number="diaFromData.gathered" min="0"></el-input>
            </el-form-item>
            <el-form-item label="已兑换数量" prop="stock">
              <el-input  v-model.number="diaFromData.exchange" min="0"></el-input>
            </el-form-item>
          </template>

          <el-form-item v-if="diaFromData.type === 2" label="优惠券类型" prop="coupon_type">
              <el-radio-group v-model="diaFromData['coupon_type']">
                <el-radio :label="1">图片</el-radio>
                <el-radio :label="2">链接</el-radio>
              </el-radio-group>
          </el-form-item>
          <el-form-item v-if="diaFromData.coupon_type === 1 && diaFromData.type === 2" label="" prop="couponImgArr">
            <div class="image-box" v-for="(e, i) in diaFromData.couponImgArr" :key="i">
              <img :src="e" alt="" height="100" style="margin-right: 8px;">
              <i class="el-icon-delete" @click="delImage(i)"></i>
            </div>
            <el-upload
              class="avatar-uploader"
              action="#"
              v-show="diaFromData.couponImgArr.length < diaFromData.stock - diaFromData.exchange"
              :show-file-list="false"
              :before-upload="beforeUpload2">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <template v-if="diaFromData.coupon_type === 2 && diaFromData.type === 2">
            <el-form-item
              v-for="index in Number(diaFromData.stock - diaFromData.exchange)"
              :key="index" :label="'链接' + index"
              :prop="'couponLink.' + (index-1) "
              :rules="{
                required: true, message: '链接不能为空', trigger: 'blur'
              }">
              <el-input v-model="diaFromData.couponLink[index-1]"></el-input>
            </el-form-item>
          </template>
          <el-form-item label="所需碎片" prop="need">
            <el-input v-model.number="diaFromData.need" ></el-input>
          </el-form-item>
          <el-form-item label="失效时间" prop="endtime">
            <div style="display: flex">
              <el-date-picker
                @change="isNoEndTime"
                v-model="diaFromData.endtime"
                type="date"
                placeholder="选择日期时间">
              </el-date-picker>
              <span style="white-space:nowrap;">&nbsp; / &nbsp;</span>
              <el-checkbox @change="isNoEndTime" v-model="diaFromData.istime">不过期</el-checkbox>
            </div>
          </el-form-item>
      </el-form>
      <!-- form end! -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addShow = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="抽奖列表"
      :visible.sync="lottery"
      >
    <div style="display: flex; flex-wrap: wrap; width: 100%;" v-if="JSON.stringify(lotteryList) !== '[]'">
      <div class="lattice" v-for="index in 9" :key="index">
        <div class="latticeIn clickable" @click="changLotteryItem(index)" v-if="index !== 5">
          <img :src="index > 4 ? lotteryList[index-2].img : lotteryList[index-1].img" width="50px;" />
          &nbsp;id: {{index > 4 ? lotteryList[index-2].prizeid : lotteryList[index-1].prizeid}}
          &nbsp;{{index > 4 ? lotteryList[index-2].name : lotteryList[index-1].name}}
        </div>
        <template v-else>
          <el-input  placeholder="请输入奖品id"  v-model="lotteryInput" @keyup.enter.native="searchIdItem" >
            <el-button slot="append" @click="searchIdItem" type="primary">确认</el-button>
          </el-input>
          <span class="lotteryTips">请点击奖品位置后输入奖品id(无中奖id为0)</span>
        </template>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="lottery = false">取 消</el-button>
      <el-button type="primary" @click="editLottery">确 定</el-button>
    </span>
  </el-dialog>

  <el-dialog
    title="无中奖配置"
    :visible.sync="showNo"
    >
      <el-form :model="diaForm" :rules="rules"  ref="bannerForm" label-width="110px">

        <el-form-item label="图片" prop="noprizeimg" style="width: 500px;">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :before-upload="beforeUpload3">
            <img v-if="diaForm.noprizeimg" width="100" :src="diaForm.noprizeimg" class="noprizeimg">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="标题" prop="noprizetitle">
          <el-input v-model="diaForm.noprizetitle">
          </el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showNo = false">取 消</el-button>
        <el-button type="primary" @click="handleClickSendForm('bannerForm')">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import utils from '@/utils/utils'
export default {
  name: 'awardList',
  data () {
    var checkProbability = (rule, value, callback) => {
      if (!value && value !== '0' && value !== 0) {
        return callback(new Error('请输入数字值'))
      }
      if (Number(value) + '' !== String(value)) {
        callback(new Error('请输入数字值'))
      } else {
        if (value < 0 || value > 100) {
          callback(new Error('概率取值在0~100之间'))
        } else {
          callback()
        }
      }
    }
    var checkEndTime = (rule, value, callback) => {
      if (!this.diaFromData.endtime && !this.diaFromData.istime) {
        return callback(new Error('请设置失效时间'))
      } else {
        callback()
      }
    }

    var checkPositiveNumber = (rule, value, callback) => {
      if (value <= 0) {
        return callback(new Error('请输入大于0的值'))
      } else {
        callback()
      }
    }

    var checkCtype = (rule, value, callback) => {
      if (this.diaFromData.type === 2 && !this.diaFromData['coupon_type']) {
        return callback(new Error('请选择优惠券类型'))
      } else {
        callback()
      }
    }

    var checkCImg = (rule, value, callback) => {
      if (this.diaFromData.type === 2 && JSON.stringify(this.diaFromData['couponImgArr']) === '[]') {
        return callback(new Error('优惠券图片不为空'))
      } else if (this.diaFromData.type === 2 && this.diaFromData['couponImgArr'].length < this.diaFromData['stock'] - this.diaFromData['exchange']) {
        return callback(new Error('优惠券图片数量应为' + this.diaFromData['stock'] - this.diaFromData['exchange']))
      } else {
        callback()
      }
    }

    return {
      sortVal: {},
      addShow: false,
      lottery: false,
      showNo: false,
      lotteryList: [],
      chooseItem: -1,
      lotteryInput: '',
      isAdd: true,
      tableData: [],
      searchItems: [
        {
          label: '奖品类型',
          type: 'select',
          key: 'type',
          options: [{ id: '1', name: '实体奖品' }, { id: '2', name: '优惠卷' }, { id: '3', name: '现金奖' }] //, { id: '4', name: '无中奖' }
        },
        {
          label: '奖品名称',
          type: 'input',
          key: 'name'
        },
        {
          label: '开始时间',
          type: 'times',
          key: 'createtime'
        },
        {
          label: '失效时间',
          type: 'times',
          key: 'endtime'
        },
        {
          label: '状态',
          type: 'select',
          key: 'status',
          options: [{ id: '1', name: '生效' }, { id: '0', name: '失效' }]
        }
      ],
      page: {
        page: 1,
        size: 20,
        total: 1
      },
      diaForm: {
        noprizeimg: '',
        noprizetitle: ''
      },
      diaFromData: {
        id: '',
        type: '', // 奖品类型
        name: '', // 奖品名称
        img: '', // 奖品图片
        stock: '', // 数量
        stockMexchange: 0,
        gathered: '', // 已集齐数量
        exchange: '', // 已兑换数量
        probability: '', // 概率
        coupon_type: '', // 优惠券类型【1-图片，2-链接】
        couponImgArr: [], // 优惠券图片
        couponLink: [], // 优惠券链接
        need: '', // 所需碎片
        istime: '', // 是否有效期【0-无限期，1-有效期】
        endtime: '', // 有效时间
        status: '', // 是否有效【0-无效，1-有效】
        reason: '' // 失效原因
      },
      rules: {
        type: [
          { required: true, message: '请选择奖品类型', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入奖品名称', trigger: 'blur' }
        ],
        img: [
          { required: true, message: '请选择奖品图片', trigger: 'blur' }
        ],
        probability: [
          { validator: checkProbability, trigger: 'blur' }
        ],
        stock: [
          { validator: checkPositiveNumber, trigger: 'blur' }
        ],
        need: [
          { validator: checkPositiveNumber, trigger: 'blur' }
        ],
        endtime: [
          { validator: checkEndTime, trigger: 'blur' }
        ],
        'coupon_type': [
          { validator: checkCtype, trigger: 'blur' }
        ],
        couponImgArr: [
          { validator: checkCImg, trigger: 'blur' }
        ],
        noprizeimg: [
          { required: true, message: '请选择图片', trigger: 'blur' }
        ],
        noprizetitle: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    getList () {
      let searchData = {}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      if (searchData.endtime) {
        searchData.endstarttime = searchData.endtime.split(',')[0]
        searchData.endendtime = searchData.endtime.split(',')[1]
        searchData.endtime = ''
      }
      if (searchData.createtime) {
        searchData.starttime = searchData.createtime.split(',')[0]
        searchData.endtime = searchData.createtime.split(',')[1]
        searchData.createtime = ''
      }
      let params = {
        ...searchData,
        page: this.page.page,
        size: this.page.size
      }
      if (this.sortVal.prop) {
        params.orderby = this.sortVal.prop + ' ' + this.sortVal.order
      }
      this.axios.get('/Luckdraw/prizeindex', { params }).then(res => {
        if (res.data.status === 1 || res.data.status === '1') {
          let arr = ['stock', 'probability', 'need', 'exchange', 'gathered']
          utils.tableItemToNumber(res.data.data.list, arr)
          // res.data.data.list.forEach(value => {
          //   for (let key in value) {
          //     if (arr.indexOf(key) > -1) {
          //       value[key] = +value[key]
          //     }
          //   }
          // })
          this.page.total = Number(res.data.data.count)
          this.tableData = res.data.data.list
        }
      })
    },
    editItem (e = 1, val) {
      let self = this
      this.cleanForm()
      this.axios.get('/Luckdraw/detprize', { params: {id: val} }).then(res => {
        if (res.data.status === 1 || res.data.status === '1') {
          let data = res.data.data
          data.istime = Number(res.data.data.istime)
          if (!res.data.data.istime) {
            res.data.data.endtime = ''
          }
          for (let key in this.diaFromData) {
            if (key !== 'couponImgArr' && key !== 'couponLink' && key !== 'istime') {
              self.diaFromData[key] = res.data.data[key]
            }
          }
          self.diaFromData.stockMexchange = self.diaFromData.stock - self.diaFromData.exchange
          // console.log(self.diaFromData.stock - self.diaFromData.exchange)
          self.diaFromData['coupon_type'] = Number(res.data.data['coupon_type'])
          self.diaFromData['type'] = Number(res.data.data['type'])
          self.diaFromData['status'] = Number(res.data.data['status'])
          if (res.data.data['coupon_type'] === 2 || res.data.data['coupon_type'] === '2') {
            setTimeout(function () {
              if (res.data.data['likelist']) {
                self.diaFromData['couponLink'] = JSON.parse(res.data.data['likelist'])
              } else {
                self.diaFromData['couponLink'] = []
              }
              // console.log(self.diaFromData['couponLink'])
            }, 100)
          } else if (res.data.data['coupon_type'] === 1 || res.data.data['coupon_type'] === '1') {
            setTimeout(function () {
              if (res.data.data['likelist']) {
                self.diaFromData['couponImgArr'] = JSON.parse(res.data.data['likelist'])
              } else {
                self.diaFromData['couponImgArr'] = []
              }
            }, 100)
          }
          if (res.data.data['istime'] !== '' || res.data.data['istime'] !== null || res.data.data['istime'] !== undefined) {
            self.diaFromData['istime'] = Boolean(Number(!(+res.data.data['istime'])))
          }
        }
      })
      this.isAdd = false
      this.addShow = true
    },
    invalidItem (e = 1, val) {
      let self = this
      this.axios.get('/Luckdraw/endprize', { params: {id: val} }).then(res => {
        if (res.data.status === 1 || res.data.status === '1') {
          self.$message({
            message: res.data.msg,
            type: 'success'
          })
          self.getList()
        }
      })
    },
    deleteItem (e = 1, val) {
      let self = this
      let params = {}
      params.id = val
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.get('/Luckdraw/delprize', { params: {id: val} }).then(res => {
          if (res.data.status === 1 || res.data.status === '1') {
            self.$message({
              message: '删除成功',
              type: 'success'
            })
            self.getList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    getLuckdrawconfig () {
      this.axios.get('/Luckdraw/luckdrawconfig', this.qs.stringify({})).then(res => {
        if (res.data.status === 1 || res.data.status === '1') {
          let lStatusText = res.data.data.luckdrawconfig ? '关闭' : '开启'
          this.$confirm('确定要' + lStatusText + '活动?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.axios.get('/Luckdraw/luckdrawsetup', this.qs.stringify()).then(res => {
              if (res.data.status === 1 || res.data.status === '1') {
                this.$message({
                  type: 'success',
                  message: lStatusText + '成功!'
                })
              }
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消操作'
            })
          })
        }
      })
    },
    handleSizeChange (val) {
      this.page.size = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.page.page = val
      this.getList()
    },
    beforeUpload (file) {
      let reader = new FileReader()
      let self = this
      reader.onload = function (e) {
        self.diaFromData.img = e.currentTarget.result
      }
      reader.readAsDataURL(file)
      return false
    },
    beforeUpload2 (file) {
      this.imageUrl = URL.createObjectURL(file)
      let _self = this
      let reader = new FileReader()
      reader.onload = function (ev) {
        _self.diaFromData.couponImgArr.push(ev.currentTarget.result)
      }
      reader.readAsDataURL(file)
      return false
    },
    beforeUpload3 (file) {
      let reader = new FileReader()
      let self = this
      reader.onload = function (e) {
        self.diaForm.noprizeimg = e.currentTarget.result
      }
      reader.readAsDataURL(file)
      return false
    },
    delImage (index) {
      this.diaFromData.couponImgArr.splice(index, 1)
    },
    changeImgOrLinkArr (val) {
      this.stockMexchange = val - this.exchange
      if (this.diaFromData.couponImgArr.length > this.stockMexchange && this.diaFromData['coupon_type'] === 1 && this.diaFromData.type === 2) {
        this.diaFromData.couponLink = []
        this.diaFromData.couponImgArr = this.diaFromData.couponImgArr.splice(0, this.stockMexchange)
      } else if (this.diaFromData.couponLink.length > this.stockMexchange && this.diaFromData['coupon_type'] === 2 && this.diaFromData.type === 2) {
        this.diaFromData.couponImgArr = []
        this.diaFromData.couponLink = this.diaFromData.couponLink.splice(0, this.stockMexchange)
      }
    },
    submitForm (formName) { // 添加/编辑提交
      let self = this
      this.diaFromData.probability = Number(this.diaFromData.probability)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {}
          for (let key in self.diaFromData) {
            if (key !== 'id' && key !== 'couponLink' && key !== 'couponImgArr') { // couponLink couponImgArr
              if (self.diaFromData[key] !== '' || key === 'endtime') {
                params[key] = self.diaFromData[key]
              }
            }
          }
          if (params['endtime']) {
            params.istime = 1
          } else {
            // params['endtime'] = ''
            params.istime = 0
            delete params['endtime']
          }
          if (!self.isAdd) {
            params['id'] = self.diaFromData.id
          }
          if (self.diaFromData.type === 2 && self.diaFromData['coupon_type'] === 1) {
            params['likelist'] = self.diaFromData.couponImgArr
          } else if (self.diaFromData.type === 2 && self.diaFromData['coupon_type'] === 2) {
            params['likelist'] = self.diaFromData.couponLink
          }
          let url = self.isAdd ? '/Luckdraw/addprize' : '/Luckdraw/editprize'
          this.axios.post(url, self.qs.stringify(params)).then(res => {
            if (res.data.status === '1' || res.data.status === 1) {
              self.$message({
                message: '成功',
                type: 'success'
              })
              self.addShow = false
              self.cleanForm()
              self.getList()
            }
          })
        } else {
          return false
        }
      })
    },
    resetForm (formName) { // 重置验证
      if (this.$refs[formName]) {
        this.$refs[formName].resetFields()
      }
    },
    isNoEndTime () {
      if (this.diaFromData.istime) {
        this.diaFromData.endtime = ''
      }
    },
    cleanForm () { // 初始化表单数据
      let self = this
      for (let key in self.diaFromData) {
        if (key !== 'couponImgArr' && key !== 'couponLink') {
          self.diaFromData[key] = ''
        }
      }
      this.diaFromData['couponLink'] = []
      this.diaFromData['couponImgArr'] = []
      this.resetForm('ruleForm')
    },
    handleAddShow () { // 显示新增框
      this.addShow = true
      this.isAdd = true
      this.cleanForm()
    },
    getLotteryList () { // 获取抽奖列表
      this.chooseItem = -1
      this.lotteryInput = ''
      this.lotteryList = []
      this.lottery = true
      this.axios.get('/Luckdraw/luckdraw', {}).then(res => {
        if (res.data.status === 1 || res.data.status === '1') {
          this.lotteryList = res.data.data
        }
      })
      let div = document.getElementsByClassName('lattice')
      for (let i = 0; i < div.length; i++) {
        div[i].className = 'lattice'
      }
    },
    changLotteryItem (val) {
      if (val > 4) {
        this.chooseItem = val - 2
      } else {
        this.chooseItem = val - 1
      }
      this.lotteryInput = this.lotteryList[this.chooseItem].prizeid
      let div = document.getElementsByClassName('lattice')
      for (let i = 0; i < div.length; i++) {
        div[i].className = 'lattice'
      }
      div[val - 1].className = 'lattice changeBorder'
    },
    searchIdItem () {
      if (this.chooseItem < 0) {
        return false
      }
      let val = this.lotteryInput
      let self = this
      if (this.lotteryInput !== 0 && this.lotteryInput !== '0') {
        this.axios.get('/Luckdraw/detprize', { params: {id: val} }).then(res => {
          if (res.data.status === 1 || res.data.status === '1') {
            let img = res.data.data ? res.data.data.img : ''
            this.lotteryList.splice(self.chooseItem, 1, { id: self.chooseItem + 1, prizeid: self.lotteryInput, img, name: res.data.data.name })
          }
        })
      } else {
        this.axios.get('/Luckdraw/noprice', { params: {id: val} }).then(res => {
          if (res.data.status === 1 || res.data.status === '1') {
            let img = res.data.data ? res.data.data.noprizeimg : ''
            this.lotteryList.splice(self.chooseItem, 1, { id: self.chooseItem + 1, prizeid: self.lotteryInput, img, name: res.data.data.noprizetitle })
          }
        })
      }
    },
    editLottery () {
      let self = this
      let params = {}
      let data = []
      for (let i = 0; i < this.lotteryList.length; i++) {
        data[i] = {
          id: self.lotteryList[i].id,
          prizeid: self.lotteryList[i].prizeid
        }
      }
      params.data = data
      this.axios.post('/Luckdraw/editluckdraw', self.qs.stringify(params)).then(res => {
        if (res.status) {
          self.$message({
            message: '成功',
            type: 'success'
          })
          self.lottery = false
        }
      })
    },
    getNoItem () {
      this.axios.get('/Luckdraw/noprice', {}).then(res => {
        if (res.data.status === 1 || res.data.status === '1') {
          // console.log(res)
          this.diaForm = res.data.data
          this.showNo = true
        }
      })
    },
    handleClickSendForm (formName) {
      let self = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {}
          for (let key in self.diaForm) {
            if (key !== 'id') {
              if (self.diaForm[key] !== '') {
                params[key] = self.diaForm[key]
              }
            }
          }
          this.axios.post('/Luckdraw/nopriceedit', self.qs.stringify(params)).then(res => {
            if (res.data.status === 1 || res.data.status === '1') {
              self.$message({
                message: '成功',
                type: 'success'
              })
              self.showNo = false
              self.getList()
            }
          })
        } else {
          return false
        }
      })
    },
    sortChange (data) {
      this.sortVal.prop = data.prop
      if (data.order === 'ascending') {
        this.sortVal.order = 'asc'
      } else if (data.order === 'descending') {
        this.sortVal.order = 'desc'
      } else {
        this.sortVal.order = ''
      }
      this.getList()
    }
  },
  mounted () {
    this.getList()
  }
}
</script>
<style scoped>
@import "../../assets/css/style.css";

</style>
